﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FD.Tools.CopyAssist.Models
{
    /**
     * Created By: Jackie Lee(天宇遊龍)
     * CreatedOn: 2018-02-06
     */
    /// <summary>
    /// 配置
    /// </summary>
    public class Config
    {
        /// <summary>
        /// 源路径
        /// </summary>
        public string SourcePath { get; set; }
        /// <summary>
        /// 目标路径
        /// </summary>
        public string TargetPath { get; set; }
        /// <summary>
        /// 忽略文件名
        /// </summary>
        public string IgnoreFiles { get; set; }
        /// <summary>
        /// 忽略文件扩展
        /// </summary>
        public string IgnoreExts { get; set; }
        /// <summary>
        /// 忽略目录名
        /// </summary>
        public string IgnoreFolders { get; set; }

        /// <summary>
        /// 是否显示文件拷贝
        /// </summary>
        public bool ShowCopying { get; set; }

        /// <summary>
        /// 忽略文件名
        /// </summary>
        public IList<string> IgnoreFileNames
        {
            get { Resolve(); return _ignoreFileNames; }
        }

        /// <summary>
        /// 忽略文件扩展
        /// </summary>
        public IList<string> IgnoreFileExts
        {
            get { Resolve(); return _ignoreExts; }
        }

        /// <summary>
        /// 忽略目录名
        /// </summary>
        public IList<string> IgnoreFolderNames { get { Resolve(); return _ignoreFolderNames; } }

        private IList<string> _ignoreFileNames, _ignoreExts, _ignoreFolderNames;

        public static Config Default()
        {
            return new Config();
        }

        /// <summary>
        /// 解析
        /// </summary>
        private void Resolve()
        {
            if (_ignoreFolderNames != null)
                return;
            _ignoreFileNames = new List<string>();
            _ignoreExts = new List<string>();
            _ignoreFolderNames = new List<string>();

            var iexts = IgnoreExts.SplitString();
            var files = IgnoreFiles.SplitString();
            var folders = IgnoreFolders.SplitString();

            AddItem(_ignoreExts, iexts, ext => ext.Trim().Replace("*", ""));
            AddItem(_ignoreFileNames, files);
            AddItem(_ignoreFolderNames, folders);
        }

        /// <summary>
        /// 添加项
        /// </summary>
        /// <param name="collect"></param>
        /// <param name="items"></param>
        /// <param name="solveAction">项处理</param>
        private void AddItem(IList<string> collect, string[] items, Func<string, string> solveAction = null)
        {
            foreach (var item in items)
            {
                collect.Add(solveAction == null ? item : solveAction.Invoke(item));
            }
        }
    }
}
